<?php
include '../sys_application.php';
include 'customerInfo.php';
include 'func.php';
	$sysApps = new sysApps();
	$sysApps->DBconnect();

	$current_month = date("n");
	$current_year = date("Y");
	$currentDate = date("Y-m-d");
	$sql = "SELECT mu.id, mu.fullname, mu.user_MGM_id
					FROM mgm_users mu
					LEFT JOIN user_commission uc
					ON mu.id = uc.id
					AND uc.month = '".$current_month."'
					AND uc.year = '".$current_year."'
					WHERE mu.payment_status = '1'
				
					";
	$query = mysql_query($sql);
	while ($row1 = mysql_fetch_array($query)) {
		
		$user_MGM_id = $row1['user_MGM_id'];
		$user_id = $row1['id'];
		$sql1 = "SELECT mu.id, mu.email, mu.url_name, mu.phone_no, mu.user_MGM_id, mu.fullname, mu.identity_no, mu.address1, mu.address2, mu.mobile_no, us.start_month, us.end_month
		FROM mgm_users mu
		LEFT JOIN user_subscription us
		ON mu.id = us.user_id
		WHERE mu.sponsor_id = '$user_MGM_id'
		AND (us.end_month = (SELECT MAX(us1.end_month)
		FROM user_subscription us1
		WHERE us1.user_id = mu.id)
		OR us.end_month IS NULL )
		GROUP BY mu.id
		ORDER BY mu.id";
		
		$result1 = mysql_query($sql1);
		
		$allInfo = new retreaveInfo(0, 0, " ");
		$allchild = array();
		$childrend = 0;
		$totalChild = 0;
		$totalActive = 0;
		$totalKey = 0;
		$totalIncome=0;
		$totalActiveKey = 0;
		$totalActiveIncome=0;
		$key = 2;
		
		while($row = mysql_fetch_array($result1))
		{	
			$retreaveInfo = new retreaveInfo(0, 0, " ");
			$retreaveInfo->setTrees("<ul><li>".$row['url_name']."<ul>");
			$allInfo = calculateIncome($row['user_MGM_id'], $retreaveInfo);
			$totalChild = $totalChild + intval($allInfo->getTotalChild());
			$totalActive = $totalActive + intval($allInfo->getTotalActive());
			$statusActive = checkStatus($row['start_month'],$row['end_month']);
			$obj=new CustomerInfo($row['id'], $row['email'], $row['url_name'], $row['phone_no'], $row['user_MGM_id'], $row['fullname'], $row['identity_no'], $row['address1'], $row['address2'], $row['mobile_no'], $row['start_month'], $allInfo->getTotalChild(), $allInfo->getTotalActive(), $statusActive);
			$allchild[] = $obj;
			$dummy = $allInfo->getTrees()."</ul></li></ul>";
			$allInfo->setTrees($dummy);
			
			// count totalKey and totalIncome
			if($key != 0)
			{
				$totalKey = $totalKey + intval($allInfo->getTotalChild());
				$totalActiveKey = $totalKey + intval($allInfo->getTotalActive());
				$key--;
			}
			else
			{
				$totalIncome = $totalIncome + intval($allInfo->getTotalChild());
				$totalActiveIncome = $totalIncome + intval($allInfo->getTotalActive());
			}
		}
		
		$entryIncome = calculateSingleEntryAndIncome($user_id, $user_MGM_id, $allchild);
		
		// insert or update DATABASE
		$queryChecking = "SELECT uc.id 
						  FROM user_commission uc
						  WHERE uc.id = '".$user_id."'
						  AND uc.month = '".$current_month."'
						  AND uc.year = '".$current_year."'";
		
		
		$resultChecking = mysql_query($queryChecking);
		if(mysql_num_rows($resultChecking) > 0)
		{
			
			$update = "UPDATE user_commission 
					SET total_commission='".$entryIncome->getTotalCommission()."', 
					modified_date = '".$currentDate."'
					WHERE id = '".$user_id."'
					AND month = '".$current_month."'
					AND year = '".$current_year."'";
			$queryChecking = mysql_query($update);
		}
		else
		{
			$Insert = "INSERT INTO user_commission
					(id,month,year,total_commission,modified_date)
					VALUES('".$user_id."','".$current_month."','".$current_year."','".$entryIncome->getTotalCommission()."','".$currentDate."')";
			$queryChecking = mysql_query($Insert);
		}
	}
	

?>